#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : Leiyh
# @File : pandas_tool.py
import os
import zipfile
import pandas as pd


# def get_test_case(file,sep=","):
#     if '.xlsx' in file:
#         file_path = file
#         data = {}
#         # 获取第一列数据
#         df = pd.read_excel(file_path, usecols=[0])
#         list1 = df.values.tolist()
#         l1 = [i[0] for i in list1]
#         data[0] = l1
#         # 获取后面所有列数据
#         xl = pd.read_excel(file_path)
#         xl = xl.iloc[:, 1:]
#         list2 = xl.values.tolist()
#         data[1] = list2
#         return data
#     if '.csv' or '.txt' in file:
#         file_path = file
#         data = {}
#         # 获取第一列数据
#         df = pd.read_csv(file_path, usecols=[0], sep=sep)
#         list1 = df.values.tolist()
#         l1 = [i[0] for i in list1]
#         data[0] = l1
#         # 获取后面所有列数据
#         xl = pd.read_csv(file_path, sep=sep)
#         xl = xl.iloc[:, 1:]
#         list2 = xl.values.tolist()
#         data[1] = list2
#         return data
#     return None


def get_test_case(file,sep=",",pos_arg_count=-1,sheet_name=0):
    """获取测试用例数据

    :param file: 用例路径,支持csv txt excel
    :param sep: 用例以指定间隔符分割
    :param pos_arg_count: 默认二维列表数据

    :return:
    """
    file_path = file
    data = {}
    if '.xlsx' in file:
        df = pd.read_excel(file_path,sheet_name)
    else: # '.csv' or '.txt' in file:
        df = pd.read_csv(file_path, sep=sep)
    data[0] = list(df.iloc[:,0])
    testdata=[]
    df_content = df.iloc[:,1:]
    df_content = df_content.fillna('')  # 将空值统一替换为空字符串
    #df_content = df_content.dropna()
    if pos_arg_count==-1:
        testdata = df_content.values.tolist()
    else:
        df_content.apply(lambda x: testdata.append((*tuple(x[:pos_arg_count]), x[pos_arg_count:].to_dict())), axis=1)
        #df_content.apply(lambda x: testdata.append((*tuple(x[:pos_arg_count]), dict((k,v.dropna().to_dict()) for k,v in x[pos_arg_count:].to_dict()))), axis=1)
    data[1] = testdata
    return data

def merge_test_case(case_data1, case_data2):
    """ 合并两组测试用例数据

    :param case_data1:
    :param case_data2:
    :return:
    """
    data = {}
    for i in range(2):
        data[i] = case_data1[i]
        data[i].extend(case_data2[i])
    return data

def data_conversion_list(data):
    data = data.replace('"','').replace("'",'').replace(" ","").strip("[]").split(",")
    return data



if __name__ == '__main__':
    root_path = os.path.split(os.path.realpath(__file__))[0]+"/"
    data1 = get_test_case(root_path + "首页_全局搜索.csv", sep="^")
    # data1 = get_test_case(root_path+"产业卡片.csv",pos_arg_count=-2)
    print(data1)
    print(data1[1])
    # print(data_conversion_list('[-1, "neeq", "NA", "HK", "B", "A"]'))
    # data_conversion_list("[20, 20, 8, 0, 0, 0, 0, 0, 0, 0]")
    # print(data_conversion_list("[5]"))


